package network;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.nio.ByteBuffer;

import network.Packet.packettype;

import vmo.Actor;
import vmo.Actor_PlayerGhost;

public class Packet_Actors extends Packet {
	//transmit state of actors

	public Actor [] m_actors;
	
	
	public Packet_Actors(int timestamp, Actor [] actors)
	{
		m_type=packettype.ACTORS;
		m_actors=actors;
		m_timestamp=timestamp;
		
	}
	
	public Packet_Actors(DatagramPacket packet) 
	{
		m_address=packet.getAddress();	
		m_type=packettype.ACTORS;
		ByteBuffer buffer=ByteBuffer.wrap(packet.getData());
		Header(packet.getAddress(),buffer);
		byte l=buffer.get();
		m_actors=new Actor[l];
		for (int i=0;i<l;i++)
		{
			byte t=buffer.get();
			switch (t)
			{
			case 0:
				m_actors[i]=new Actor_PlayerGhost(buffer);
				break;
			
			}
			
		}
		
	}	
	
	public DatagramPacket Encode(InetAddress address, int port)
	{
		byte []buffer=new byte[1024];
		ByteBuffer BB=ByteBuffer.wrap(buffer);	
		//encode type
		BB.put(getTypebyte(m_type));	
		BB.putInt(m_timestamp);	

		BB.put((byte) m_actors.length);
		for (int i=0;i<m_actors.length;i++)
		{
			BB.put(m_actors[i].getAtype());
			m_actors[i].Encode(BB);			
		}
		return new DatagramPacket(buffer,buffer.length,address,port);				
	}
}
